#include "BMP180.h"

short ac1;
short ac2; 
short ac3; 
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1; 
short b2;
short mb;
short mc;
short md;

long ut;
long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
long  temperature;
long  pressure;

void SDA_OUT(void)   
{
    DL_GPIO_initDigitalOutput(GPIO_SDA_PIN_1_IOMUX);     
	DL_GPIO_setPins(GPIO_SDA_PORT, GPIO_SDA_PIN_1_PIN);	   
    DL_GPIO_enableOutput(GPIO_SDA_PORT, GPIO_SDA_PIN_1_PIN); 
}

void SDA_IN(void)
{
    DL_GPIO_initDigitalInputFeatures(GPIO_SDA_PIN_1_IOMUX,
		 DL_GPIO_INVERSION_DISABLE, DL_GPIO_RESISTOR_PULL_UP,
		 DL_GPIO_HYSTERESIS_DISABLE, DL_GPIO_WAKEUP_DISABLE);
}


void Delay5us(){
	delay_us(5);
}

void Delay5ms(){
	delay_ms(5);
}

//conversion

void BMP085_Start()
{
	SDA_OUT();
    BMP_SDA_Set();                   
    BMP_SCL_Set();
    Delay5us();                
    BMP_SDA_Clr();                  
    Delay5us();                
    BMP_SCL_Clr();                
}
void BMP085_Stop()
{
	SDA_OUT();
    BMP_SDA_Clr();                   
    BMP_SCL_Set();                
    Delay5us();                
    BMP_SDA_Set();                     
    Delay5us();                
}


void BMP085_SendACK(uint8_t ack)
{
	SDA_OUT();
    if(ack){
		BMP_SDA_Set(); 
	}
	else{
		BMP_SDA_Clr();
	}
    BMP_SCL_Set();                 
    Delay5us();                 
    BMP_SCL_Clr();                
    Delay5us();                
}

uint8_t MyI2C_R_SDA(void)
{
	uint8_t b;
    uint32_t BitValue;
    SDA_IN();
	BitValue = DL_GPIO_readPins(GPIO_SDA_PORT, GPIO_SDA_PIN_1_PIN);	
    {
        if(BitValue)   b=1;
        else           b=0;
    }
	//delay_us(8);		
	return b;	        
}

uint8_t  BMP085_RecvACK()
{
	SDA_IN();
    BMP_SCL_Set();                
    Delay5us(); 
	uint8_t ackbit;
    ackbit = MyI2C_R_SDA();                   
    BMP_SCL_Clr();                     
    Delay5us();                 
    return ackbit;
}


void BMP085_SendByte(BYTE dat)
{
    BYTE i;
	SDA_OUT();
    for (i=0; i<8; i++)        
    {
		if(dat & (0x80 >> i)){
			BMP_SDA_Set(); 
		}
		else{
			BMP_SDA_Clr();
		}
        BMP_SCL_Set();              
        Delay5us();            
        BMP_SCL_Clr();                  
        Delay5us();             
    }
    BMP085_RecvACK();
}
uint8_t BMP085_RecvByte()
{
    BYTE i;
    BYTE dat = 0;
	SDA_IN();
    BMP_SDA_Set();                   
    for (i=0; i<8; i++)        
    {
        dat <<= 1;
        BMP_SCL_Set();                
        Delay5us();            
        dat |= MyI2C_R_SDA();                          
        BMP_SCL_Clr();               
        Delay5us();            
    }
    return dat;
}
short Multiple_read(unsigned char ST_Address)
{   
	unsigned char msb, lsb;
	short _data;
    BMP085_Start();                          //????
    BMP085_SendByte(BMP085_SlaveAddress);    //??????+???
    BMP085_SendByte(ST_Address);             //????????
    BMP085_Start();                          //????
    BMP085_SendByte(BMP085_SlaveAddress+1);         //??????+???

    msb = BMP085_RecvByte();                 //BUF[0]??
    BMP085_SendACK(0);                       //??ACK
    lsb = BMP085_RecvByte();     
	BMP085_SendACK(1);                       //?????????NOACK

    BMP085_Stop();                           //????
    Delay5ms();
    _data = msb << 8;
	_data |= lsb;	
	return _data;
}

long bmp085ReadTemp(void)
{

    BMP085_Start();                  //????
    BMP085_SendByte(BMP085_SlaveAddress);   //??????+???
    BMP085_SendByte(0xF4);	          // write register address
    BMP085_SendByte(0x2E);       	// write register data for temp
    BMP085_Stop();                   //??????
	delay_ms(3);	// max time is 4.5ms
	
	return (long) Multiple_read(0xF6);
}

long bmp085ReadPressure(void)
{
	long pressure = 0;

    BMP085_Start();                   //????
    BMP085_SendByte(BMP085_SlaveAddress);   //??????+???
    BMP085_SendByte(0xF4);	          // write register address
    BMP085_SendByte(0x34);       	  // write register data for pressure
    BMP085_Stop();                    //??????
	delay_ms(3);    	                  // max time is 4.5ms
	
	pressure = Multiple_read(0xF6);
	pressure &= 0x0000FFFF;
	
	return pressure;	
	//return (long) bmp085ReadShort(0xF6);
}


void Init_BMP085()
{
	ac1 = Multiple_read(0xAA);
	ac2 = Multiple_read(0xAC);
	ac3 = Multiple_read(0xAE);
	ac4 = Multiple_read(0xB0);
	ac5 = Multiple_read(0xB2);
	ac6 = Multiple_read(0xB4);
	b1 =  Multiple_read(0xB6);
	b2 =  Multiple_read(0xB8);
	mb =  Multiple_read(0xBA);
	mc =  Multiple_read(0xBC);
	md =  Multiple_read(0xBE);
}


long bmp085Convert_temp()
{
	
	
	ut = bmp085ReadTemp();
	ut = bmp085ReadTemp();	   // ????
	up = bmp085ReadPressure();
	up = bmp085ReadPressure();  // ????
	
	x1 = ((long)ut - ac6) * ac5 >> 15;
	x2 = ((long) mc << 11) / (x1 + md);
	b5 = x1 + x2;
	temperature = (b5 + 8) >> 4;
	return temperature;
}
long bmp085Convert_press()
{
//	long ut;
//	long up;
//	long x1, x2, b5, b6, x3, b3, p;
//	unsigned long b4, b7;
//	long  temperature;
//	long  pressure;
	
	ut = bmp085ReadTemp();
	ut = bmp085ReadTemp();	   // ????
	up = bmp085ReadPressure();
	up = bmp085ReadPressure();  // ????
	
//	x1 = ((long)ut - ac6) * ac5 >> 15;
//	x2 = ((long) mc << 11) / (x1 + md);
//	b5 = x1 + x2;
//	temperature = (b5 + 8) >> 4;
	//conversion(temperature);
	b6 = b5 - 4000;
	x1 = (b2 * (b6 * b6 >> 12)) >> 11;
	x2 = ac2 * b6 >> 11;
	x3 = x1 + x2;
	b3 = (((long)ac1 * 4 + x3) + 2)/4;
	x1 = ac3 * b6 >> 13;
	x2 = (b1 * (b6 * b6 >> 12)) >> 16;
	x3 = ((x1 + x2) + 2) >> 2;
	b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
	b7 = ((unsigned long) up - b3) * (50000 >> OSS);
	if( b7 < 0x80000000){
	     p = (b7 * 2) / b4 ;
	}
    else{  
		    p = (b7 / b4) * 2;
	}
	x1 = (p >> 8) * (p >> 8);
	x1 = (x1 * 3038) >> 16;
	x2 = (-7357 * p) >> 16;
	pressure = p + ((x1 + x2 + 3791) >> 4);
	return pressure;
	//conversion(pressure);
}

